package com.smag.web.controller.system;

import com.smag.common.annotation.Log;
import com.smag.common.config.RuoYiConfig;
import com.smag.common.core.controller.BaseController;
import com.smag.common.core.domain.AjaxResult;
import com.smag.common.core.domain.entity.FarmArticle;
import com.smag.common.core.page.TableDataInfo;
import com.smag.common.enums.BusinessType;
import com.smag.common.utils.file.FileUploadUtils;
import com.smag.common.utils.file.MimeTypeUtils;
import com.smag.common.utils.poi.ExcelUtil;
import com.smag.system.domain.dto.ArticleQueryDto;
import com.smag.system.domain.vo.FarmArticleVo;
import com.smag.system.service.FarmArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/system/article")
public class SysArticleController extends BaseController {
    @Resource
    private FarmArticleService farmArticleService;

    @PostMapping("/avatar")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    public AjaxResult avatar(@RequestParam("articleId") Long articelId ,@RequestParam("avatarfile") MultipartFile file) throws Exception {
        if (!file.isEmpty()){
            String avatar = FileUploadUtils.upload(RuoYiConfig.getIllustrationPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (farmArticleService.updateArticleAvatar(articelId, avatar)) {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArticleQueryDto articleQueryDto)
    {
        startPage();
        List<FarmArticle> list = farmArticleService.selectFarmArticleList(articleQueryDto);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArticleQueryDto articleQueryDto)
    {
        List<FarmArticle> list = farmArticleService.selectFarmArticleList(articleQueryDto);
        ExcelUtil<FarmArticle> util = new ExcelUtil<FarmArticle>(FarmArticle.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return farmArticleService.selectFarmArticleById(id);
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FarmArticleVo farmArticleVo)
    {
        return farmArticleService.insertFarmArticle(farmArticleVo);
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FarmArticleVo farmArticleVo)
    {
        return toAjax(farmArticleService.updateFarmArticle(farmArticleVo));
    }
//
    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return farmArticleService.deleteFarmArticleByIds(ids);
    }
}
